import 'package:flutter/material.dart';
import 'package:flutter_fun_zjversion/generated/i18n.dart';
import 'package:flutter_fun_zjversion/ui/page/project_page.dart';
import 'package:flutter_fun_zjversion/ui/page/structure_page.dart';
import 'package:flutter_fun_zjversion/ui/page/user.dart';
import 'package:flutter_fun_zjversion/ui/page/wechat_account_page.dart';
import 'package:flutter_fun_zjversion/ui/widget/app_update.dart';
import 'package:flutter_fun_zjversion/view_model/app_model.dart';

import 'home_page.dart';

List<Widget> pages = [
  HomePage(),
  ProjectPage(),
  WechatAccountPage(),
  StructurePage(),
  UserPage()
];

class TabNavigator extends StatefulWidget {
  @override
  TabNavigatorState createState() => new TabNavigatorState();

  TabNavigator({Key key}) : super(key: key);
}

class TabNavigatorState extends State<TabNavigator> {
  int _selectedIndex = 0;
  PageController _pageController = PageController();
  DateTime _lastPressed;
  @override
  Widget build(BuildContext context) {


    return new Scaffold(
      body: WillPopScope(
          child: PageView.builder(
            itemBuilder: (ctx, index) => pages[index],
            itemCount: pages.length,
            controller: _pageController,
            physics: NeverScrollableScrollPhysics(),
            onPageChanged: (index){
              setState(() {
                _selectedIndex = index;
              });
            },
          ),
          onWillPop: () async {
            if (_lastPressed == null ||
                DateTime.now().difference(_lastPressed) >
                    Duration(seconds: 1)) {
              _lastPressed = DateTime.now();
              return false;
            } else {
              return true;
            }
          }),
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text(S.of(context).tabHome)
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.format_list_bulleted),
              title: Text(S.of(context).tabProject,)
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.group_work),
                title: Text(S.of(context).wechatAccount)
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.call_split),
              title: Text(S.of(context).tabStructure)
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.insert_emoticon),
              title: Text(S.of(context).tabUser)
            )

          ],
        currentIndex: _selectedIndex,
        onTap: (index){
            _pageController.jumpToPage(index);
        },
      ),
    );
  }

  @override
  void initState() {
    checkAppUpdate();
    super.initState();
  }

  void checkAppUpdate() async{
    String url = await AppUpdateModel().checkUpdate();
    if (url != null) {
      bool result = await showUpdateDialog(context);
      if (result == true) downloadApp(context, url);
    }
  }
}
